package com.sali.b_design_pattern.g_singleton_pattern.lesson_destroy_singleton.b_serialize;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

/**
 * @description:
 * @date: 2023/3/19 18:25.
 * @version: 1.0
 */
public class Test {

    public static void main(String[] args) throws Exception {
        // 创建对象的方式
        // 方式1：new
        User user = User.builder().id(1L).username("小A").build();
        System.out.println("new方式创建对象：" + user);

        // 方式2：反射
        User user1 = (User) Class.forName("com.sali.design_pattern.g_singleton_pattern.lesson_destroy_singleton.b_serialize.User").newInstance();
        System.out.println("反射方式创建对象：" + user1);

        // 方式3：流方式创建对象
        FileOutputStream fos = new FileOutputStream("User.obj");
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        oos.writeObject(user);
        oos.flush();
        oos.close();
        FileInputStream fis = new FileInputStream("User.obj");
        ObjectInputStream ois = new ObjectInputStream(fis);
        User user2 = (User) ois.readObject();
        ois.close();
        System.out.println("序列化方式创建对象：" + user2);
    }

}
